package elcs.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import elcs.board.model.ListVO;
import elcs.group.model.GroupService;
import elcs.group.model.GroupVO;
import elcs.member.model.MemberService;
import elcs.member.model.MemberVO;
import elcs.point.model.TradePointService;
import elcs.point.model.TradePointVO;

@Controller
public class MemberController {
	@Resource(name="memberServiceImpl")
	private MemberService memberService;
	@Resource(name="tradePointServiceImpl")
	private TradePointService tradePointService;
	@Resource(name="groupServiceImpl")
	private GroupService groupService;
	
	//이정현
	@RequestMapping("/member/mypage.do")
	public String mypageView(){
		System.out.println("mypageView");
		
		return "mypage.member";
	}
	@RequestMapping("/member/mypageMyInfo.do")
	public ModelAndView mypageMyInfo(HttpSession session){
		System.out.println("myInfoView");
		System.out.println((MemberVO)session.getAttribute("mvo"));
		return new ModelAndView("mypage_info.member","info",(MemberVO)session.getAttribute("mvo"));
	}
	@RequestMapping("/member/mypageUpdateView.do")
	public ModelAndView mypageUpdateView(HttpSession session, MemberVO mvo){
		System.out.println("myUpdateView");
		
		return new ModelAndView("mypage_update.member","info",(MemberVO)session.getAttribute("mvo"));
	}
	@RequestMapping("/member/mypageUpdate.do")
	public ModelAndView mypageUpdate(HttpSession session, MemberVO mvo){
		System.out.println("myUpdate");
	
		memberService.mypageUpdate(mvo);
		session.setAttribute("mvo", mvo);
		
		return new ModelAndView("mypage_update_result.member","info",(MemberVO)session.getAttribute("mvo"));
	}
	@RequestMapping("/member/mypageMyGroup.do")
	public ModelAndView mypageGroup(HttpSession session,HttpServletRequest request){
		System.out.println("myGroupView");
		
		List<GroupVO> gvo=groupService.mypageMyGroup();
		System.out.println(gvo);
		
		return new ModelAndView("mypage_group.member","gvo",gvo);
	}
	@RequestMapping("/member/mypageMyPoint.do")
	public ModelAndView mypagePoint(TradePointVO vo,HttpServletRequest request) throws Exception{
		
		String pageNo=request.getParameter("pageNo");
		System.out.println(pageNo+"<-pageNo");
		Map map=new HashMap();
		map.put("id", vo.getId());
		map.put("pageNo",pageNo);
		
		ListVO lvo=tradePointService.mypagePointList(map);
		System.out.println(lvo);
		
		return new ModelAndView("mypage_point.member","lvo",lvo);
	}
	@RequestMapping("/member/mypageDeleteView.do")
	public ModelAndView mypageDeleteView(HttpSession session){
		System.out.println("myDeleteView");
			
		return new ModelAndView("mypage_delete.member");
	}
	@RequestMapping("/member/mypageDelete.do")
	public String mypageDelete(HttpSession session, MemberVO mvo){
		System.out.println("myDelete");
		
		mvo=(MemberVO)session.getAttribute("mvo");
		System.out.println(mvo);
		memberService.mypageDelete(mvo);
		session.invalidate();
	
		return "redirect:../main.do";
	}
	@Transactional
	@RequestMapping("/member/createGroup.do")
	public String createGroup(GroupVO gvo, HttpSession session) throws Exception{
		System.out.println("createGroup");		
		groupService.createGroup(gvo,session);
		tradePointService.minusPoint(session, "그룹 창설",1000);
		return "mypage.member";
	}
	@RequestMapping("/member/groupCreateView.do")
	public String groupcreateView()
	{
		System.out.println("groupCreateView");
		
		return "mypage_group_create.member";
	}
	
	@RequestMapping("/member/groupJoinMember.do")
	public String groupJoinMember(HttpSession session,String gname)
	{
		System.out.println("groupJoinMember  "+gname );
		
		memberService.updateMemberByGroup(session,gname);
		
		
		return "redirect:/member/mypageMyGroup.do";
	}
	
	
	
	//최성찬
	@ResponseBody
	@RequestMapping("/login.do")
	public boolean loginAjax(HttpSession session,MemberVO mvo)
	{
		System.out.println("login  " +mvo);
		boolean flag=true;
		
		mvo=memberService.login(session,mvo);
		if(mvo==null)
		{
			flag=false;
		}
		System.out.println(mvo);
		System.out.println(flag);
		
		
		return flag;
	}
	@RequestMapping("/member/logout.do")
	public String logout(HttpSession session)
	{
		System.out.println("logout");
		memberService.logout(session);
		
		return "redirect:../main.do";
	}
	
	@RequestMapping("/register.do")
	public String register(MemberVO mvo)
	{
		System.out.println("register");
		memberService.register(mvo);
		
		return "redirect:/main.do";
	}
	@ResponseBody
	@RequestMapping("/checkMemberById.do")
	public boolean checkMemberByIdAjax(String id)
	{
		System.out.println(id);
		System.out.println("checkMemberByIdAjax");
		return memberService.checkMemberById(id);
	}
	
	@ResponseBody
	@RequestMapping("/checkMemberByNick.do")
	public boolean checkMemberByNickAjax(String nick)
	{
		System.out.println("checkMemberByNickAjax");
		return memberService.checkMemberByNick(nick);
	}
	
	@ResponseBody
	@RequestMapping("/checkMemberBySsn.do")
	public boolean checkMemberBySsnAjax(String ssn)
	{
		System.out.println("checkMemberBySsnAjax");
		return memberService.checkMemberBySsn(ssn);
	}
	
	
	
	
	@ResponseBody
	@RequestMapping("/suchMember.do")
	public Map suchMemberAjax(String method,MemberVO mvo)
	{
		System.out.println("suchMemberAjax " + method+"  "+mvo);
		Map map=memberService.suchMember(method,mvo);
		System.out.println(map);
		
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/memberUpdateByPassword.do")
	public boolean memberUpdateByPasswordAjax(MemberVO mvo)
	{
		System.out.println("memberUpdateByPasswordAjax");
		System.out.println(mvo);
		boolean result=memberService.updateMemberByPassword(mvo);
		
		
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/checkGroupByName.do")
	public boolean checkGroupByNameAjax(String gname)
	{
		System.out.println(gname);
		System.out.println("checkGroupByNameAjax");
		return groupService.checkGroupByName(gname);
	}
	

	@ResponseBody
	@RequestMapping("/member/memberlist.do")
	public List memberlist(String gname)
	{
		System.out.println("memberlist  "+gname);
		List list= memberService.findByMemberGroup(gname);
		System.out.println(list);
		return list;
	}
	@RequestMapping("/member/groupUpdateView.do")
	public ModelAndView groupUpdateView(HttpSession session){
		System.out.println("groupUpdateView");
		MemberVO mvo=(MemberVO)session.getAttribute("mvo");
		System.out.println(mvo);
		Map map=groupService.findGroupByName(mvo.getGvo().getGname());
		System.out.println(map);
		return new ModelAndView("mypage_group_update.member","map",map);
		
	}
	@ResponseBody
	@RequestMapping("/member/groupBan.do")
	public void groupBan(HttpSession session,String id){
		System.out.println("groupBan");
		memberService.groupBan(session,id);
		
	}
	
	
	
	
	
	
	

	
}
